extends "res://BaseEnemy.gd"

# 精英雪山雪人特有属性
@export var avalanche_cooldown = 10.0  # 雪崩冷却时间
@export var avalanche_damage = 30  # 雪崩伤害
@export var avalanche_radius = 150.0  # 雪崩范围
@export var ice_wall_cooldown = 15.0  # 冰墙冷却时间
@export var ice_wall_duration = 5.0  # 冰墙持续时间
@export var snowball_volley_cooldown = 6.0  # 雪球齐射冷却时间
@export var snowball_count = 5  # 雪球数量
@export var snowball_damage = 8  # 雪球伤害

var avalanche_timer = 0.0
var ice_wall_timer = 0.0
var snowball_volley_timer = 0.0
var can_avalanche = true
var can_ice_wall = true
var can_snowball_volley = true
var ice_walls = []

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置精英雪山雪人特有属性
	max_health = 200
	current_health = max_health
	movement_speed = 60.0
	damage = 18
	experience_value = 40
	is_elite = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.8, 0.9, 1.2)  # 精英雪人颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理雪崩冷却
	if not can_avalanche:
		avalanche_timer += delta
		if avalanche_timer >= avalanche_cooldown:
			can_avalanche = true
			avalanche_timer = 0.0
	
	# 处理冰墙冷却
	if not can_ice_wall:
		ice_wall_timer += delta
		if ice_wall_timer >= ice_wall_cooldown:
			can_ice_wall = true
			ice_wall_timer = 0.0
	
	# 处理雪球齐射冷却
	if not can_snowball_volley:
		snowball_volley_timer += delta
		if snowball_volley_timer >= snowball_volley_cooldown:
			can_snowball_volley = true
			snowball_volley_timer = 0.0
	
	# 清理已销毁的冰墙
	var active_walls = []
	for wall in ice_walls:
		if is_instance_valid(wall):
			active_walls.append(wall)
	ice_walls = active_walls
	
	# 如果可以使用雪崩且玩家存在，尝试使用雪崩
	if can_avalanche and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 200 and randf() < 0.02:  # 每帧2%几率
			trigger_avalanche()
	
	# 如果可以使用冰墙且玩家存在，尝试使用冰墙
	if can_ice_wall and target_player and is_instance_valid(target_player) and ice_walls.size() < 2:
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 150 and randf() < 0.01:  # 每帧1%几率
			create_ice_wall()
	
	# 如果可以使用雪球齐射且玩家存在，尝试使用雪球齐射
	if can_snowball_volley and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 250 and distance > 50 and randf() < 0.03:  # 每帧3%几率
			snowball_volley()

func trigger_avalanche():
	if animation_player and animation_player.has_animation("avalanche"):
		animation_player.play("avalanche")
	
	# 创建雪崩效果
	var avalanche = preload("res://AvalancheEffect.tscn").instantiate()
	get_tree().get_root().add_child(avalanche)
	avalanche.global_position = global_position
	avalanche.damage = avalanche_damage
	avalanche.radius = avalanche_radius
	avalanche.source = self
	
	# 重置雪崩冷却
	can_avalanche = false
	avalanche_timer = 0.0
	
	print("精英雪人触发雪崩!")

func create_ice_wall():
	if animation_player and animation_player.has_animation("ice_wall"):
		animation_player.play("ice_wall")
	
	# 计算冰墙位置（玩家和雪人之间）
	var wall_position = Vector2.ZERO
	if target_player and is_instance_valid(target_player):
		var direction = (target_player.global_position - global_position).normalized()
		wall_position = global_position + direction * 50
	
	# 创建冰墙
	var ice_wall = preload("res://IceWall.tscn").instantiate()
	get_tree().get_root().add_child(ice_wall)
	ice_wall.global_position = wall_position
	ice_wall.duration = ice_wall_duration
	ice_wall.source = self
	
	# 添加到冰墙列表
	ice_walls.append(ice_wall)
	
	# 重置冰墙冷却
	can_ice_wall = false
	ice_wall_timer = 0.0
	
	print("精英雪人创建冰墙!")

func snowball_volley():
	if animation_player and animation_player.has_animation("volley"):
		animation_player.play("volley")
	
	# 计算基础方向（朝向玩家）
	var base_direction = Vector2.RIGHT
	if target_player and is_instance_valid(target_player):
		base_direction = (target_player.global_position - global_position).normalized()
	
	# 发射多个雪球
	for i in range(snowball_count):
		# 计算扩散角度
		var angle_offset = deg_to_rad(60) * (i / float(snowball_count - 1) - 0.5) * 2.0
		
		# 旋转基础方向
		var direction = base_direction.rotated(angle_offset)
		
		# 创建雪球投射物
		var snowball = preload("res://SnowballProjectile.tscn").instantiate()
		get_tree().get_root().add_child(snowball)
		snowball.global_position = global_position
		
		# 设置雪球属性
		snowball.direction = direction
		snowball.speed = 200.0
		snowball.damage = snowball_damage
		snowball.slow_factor = 0.7
		snowball.slow_duration = 2.0
		snowball.source = self
		
		# 延迟发射
		snowball.position.y -= 1000  # 先移到视野外
		var tween = create_tween()
		tween.tween_property(snowball, "position:y", snowball.position.y + 1000, 0.1 * i)  # 依次出现
	
	# 重置雪球齐射冷却
	can_snowball_volley = false
	snowball_volley_timer = 0.0
	
	print("精英雪人发射雪球齐射!")

# 重写攻击方法
func attack_player():
	# 随机选择一种攻击
	var attack_type = randi() % 3
	
	match attack_type:
		0:
			if can_avalanche:
				trigger_avalanche()
				return
		1:
			if can_ice_wall:
				create_ice_wall()
				return
		2:
			if can_snowball_volley:
				snowball_volley()
				return
	
	# 如果选择的攻击不可用，使用普通攻击
	super.attack_player()
